unit libMaintenance_CustomD;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, ADODB, LibTypesU, Variants, libBasic_CustomD, FireDAC.Stan.Intf,
  FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
  FireDAC.Stan.Async, FireDAC.DApt,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.Phys.Intf,
  FireDAC.DApt.Intf;

type
  TdmMaintenance_Custom = class(TdmBasic_Custom)
    qryMaster: TFDQuery;
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
    procedure dsMasterAfterInsert(DataSet: TDataSet);
  private
    procedure doCalaceField;
  protected
    lstDataset:TList;
    lstCalcField: TList;
    lstDataSetCommandText: TStringList;
    procedure doAddSaveList(ds:TFDQuery);
    procedure doDeleteSaveList(ds:TFDQuery);
    procedure doAddCalcField;virtual;
  public
    bGetText: boolean;
    fldKeyFieldName: string;
    property PriceVisible;

    function DataSetsModified: Boolean;
    procedure doNewHistory;
    procedure doEditHistory;
    procedure doCloseAllDataSet;
    procedure doDeleteAllRecord(iFirst: integer = 0);
    procedure doRefreshAllDataSet;
    procedure doUpdateBatchDataSet;
    procedure doCancelBatchDataSet;
    procedure doDisableAllDataSet(bDisable:boolean);
    procedure doUpdateKeyValue(sKeyFieldValue:string);
    procedure doOpenAllDataSet(sKeyFieldValue:string);
  end;

implementation

uses
  libParamsU, MainModule;

{$R *.DFM}

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doAddCalcField;
begin

end;

procedure TdmMaintenance_Custom.doAddSaveList(ds: TFDQuery);
begin
  lstDataset.Add(ds);
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.DataModuleCreate(Sender: TObject);
begin
  inherited;

  lstDataset := TList.Create;
  lstDataSetCommandText := TStringList.Create;
  doAddSaveList(qryMaster);
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.DataModuleDestroy(Sender: TObject);
begin
  lstDataset.Free;
  
  inherited;
end;

function TdmMaintenance_Custom.DataSetsModified: Boolean;
var
  iCount: integer;
  aDataSet_: TFDQuery;
begin
  Result := False;
  for iCount := 0 to lstDataset.Count - 1 do
  begin
    aDataSet_ := lstDataSet.Items[iCount];
    if aDataSet_.Modified  then
      Result := True;
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doOpenAllDataSet(sKeyFieldValue:string);
var
  iCount,iField,iRecNo: integer;
  aField: TField;
  sParamName: string;
  aDataSet_: TADODataSet;
begin
  lstDataSetCommandText.Clear;
  sParamName := copy(fldKeyFieldName, 4, MaxInt);
  for iCount := 0 to lstDataset.Count - 1 do
  begin
    aDataSet_ := lstDataset.Items[iCount];
    lstDataSetCommandText.Add(aDataSet_.CommandText);
    aDataSet_.Close;
    aDataSet_.Filter := '';
    aDataSet_.Filtered := False;
    if fldKeyFieldName <> '' then
      aDataSet_.Parameters.ParamByName(sParamName).Value := sKeyFieldValue;
//    aDataSet_.AutoCalcFields := True;
    aDataSet_.Open;
//    aDataSet_.AutoCalcFields := False;
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doCloseAllDataSet;
var
  iCount: integer;
  aDataSet_: TFDQuery;
begin
  for iCount := 0 to lstDataset.Count - 1 do
  begin
    aDataSet_ := lstDataSet.Items[iCount];
    aDataSet_.Close;
    aDataSet_.Filter := '';
    aDataSet_.Filtered := False;
    if lstDataset.Count =lstDataSetCommandText.Count then
    begin
      aDataSet_.SQL.Text := lstDataSetCommandText.Strings[iCount];
    end;
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doDeleteAllRecord(iFirst: integer = 0);
var
  iCount: integer;
  aDataSet_: TFDQuery;
begin
  for iCount := iFirst to lstDataset.Count - 1 do
  begin
    aDataSet_ := lstDataSet.Items[iCount];

    if SameText(aDataSet_.Name, 'qryMaster') then
    begin
      aDataSet_.Delete;
      Continue;
    end;

    aDataSet_.Filter := '';
    aDataSet_.Filtered := False;
    aDataSet_.First;
    while not aDataSet_.Eof do
      aDataSet_.Delete;
  end;
end;

procedure TdmMaintenance_Custom.doDeleteSaveList(ds: TFDQuery);
begin
  lstDataset.Delete(lstDataset.IndexOf(ds));
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doUpdateBatchDataSet;
var
  iCount: integer;
  aDataSet_: TFDQuery;
begin
  for iCount := 0 to lstDataset.Count - 1 do
  begin
    aDataSet_ := lstDataSet.Items[iCount];
    if aDataSet_.State in [dsInsert, dsEdit] then
      aDataSet_.Post;
    aDataSet_.Filter := '';
    aDataSet_.Filtered := False;
    aDataSet_.ApplyUpdates();
    aDataSet_.CommitUpdates;
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doCalaceField;
begin

end;

procedure TdmMaintenance_Custom.doCancelBatchDataSet;
var
  iCount: integer;
  aDataSet_: TFDQuery;
begin
  for iCount := 0 to lstDataset.Count - 1 do
  begin
    aDataSet_ := lstDataSet.Items[iCount];
    aDataSet_.Filter := '';
    aDataSet_.Filtered := False;
    aDataSet_.CancelUpdates
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doUpdateKeyValue(sKeyFieldValue: string);
var
  iCount: integer;
  aDataSet_: TFDQuery;
begin
  for iCount := 0 to lstDataset.Count - 1 do
  begin
    aDataSet_ := lstDataset.Items[iCount];
    aDataSet_.Filter := '';
    aDataSet_.Filtered := False;
    aDataSet_.First;
    while not aDataSet_.Eof do
    begin
      aDataSet_.Edit;
      aDataSet_[fldKeyFieldName] := sKeyFieldValue;
      aDataSet_.Post;
      aDataSet_.Next;
    end;
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doEditHistory;
begin
  if not (qryMaster.State in [dsEdit,dsInsert]) then
    qryMaster.Edit;
  qryMaster['fldLastModifier'] := sUserCode;
  qryMaster['fldLastModTime'] := dDateTime;
  qryMaster['fldNoOfMod'] := qryMaster['fldNoOfMod'] + 1;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doNewHistory;
begin
  if qryMaster.State in [dsInsert, dsEdit] then
  begin
    qryMaster['fldCreator'] := sUserCode;
    qryMaster['fldCreateTime'] := dDateTime;
    qryMaster['fldLastModifier'] := sUserCode;
    qryMaster['fldLastModTime'] := dDateTime;
    qryMaster['fldNoOfMod'] := 1;
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doDisableAllDataSet(bDisable:boolean);
var
  iCount: integer;
  aDataSet: TFDQuery;
begin
  try
    for iCount := 0 to lstDataSet.Count - 1 do
    begin
      aDataSet := lstDataSet.Items[iCount];
      if bDisable then
        aDataSet.DisableControls
      else
        aDataSet.EnableControls;
    end;
  except
    on E: Exception do
      raise Exception.Create(E.Message);
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.dsMasterAfterInsert(DataSet: TDataSet);
begin
  inherited;

end;

{------------------------------------------------------------------------------}
procedure TdmMaintenance_Custom.doRefreshAllDataSet;
var
  iCount: integer;
  aDataSet_: TFDQuery;
begin
  for iCount := 0 to lstDataset.Count - 1 do
  begin
    aDataSet_ := lstDataSet.Items[iCount];
    aDataSet_.Refresh;
  end;
end;

end.
